unit JATUIF_Component;

{$mode objfpc}{$H+}

interface

uses
    Classes, SysUtils, JATUIF_Driver_Screen;

type

    { TComponent }

    TComponent = class(TObject)
      protected
        Screen : TScreen;
        sX1,sY1,sX2,sY2 : Integer;
        Constructor Create(Parent:TScreen; X1,Y1,X2,Y2:Integer);
        Procedure _OnKeyPress(Key:Char); virtual;
        Procedure _OnMouseEnter; virtual;
        Procedure _OnMouseLeave; virtual;
        Procedure _OnClick; virtual;
        Procedure _OnLoseFocus; virtual;
        Procedure Draw; virtual; abstract;
        Procedure SetVisible(Vis:Boolean); virtual;
      Private
        Tag : Integer;
        cb_OnKeyPress : TComponent_cb_KeyPress;
        cb_OnMouseEnter : TComponent_cb_MouseEnter;
        cb_OnMouseLeave : TComponent_cb_MouseLeave;
        cb_OnClick : TComponent_Focus;
        cb_OnLoseFocus : TComponent_UnFocus;
        Procedure SetOnKeyPress(CB:TComponent_cb_KeyPress);
        Function GetOnKeyPress:TComponent_cb_KeyPress;
        Procedure SetOnMouseEnter(CB:TComponent_cb_MouseEnter);
        Function GetOnMouseEnter:TComponent_cb_MouseEnter;
        Procedure SetOnMouseLeave(CB:TComponent_cb_MouseLeave);
        Function GetOnMouseLeave:TComponent_cb_MouseLeave;
        Procedure SetOnClick(CB:TComponent_Focus);
        Function GetOnClick:TComponent_Focus;
        Procedure SetOnLoseFocus(CB:TComponent_UnFocus);
        Function GetOnLoseFocus:TComponent_UnFocus;
        Function GetVisible:Boolean;
        Procedure _DummyMouse;
        Procedure _DummyKeyboard(Key:Char);
      Public
        Destructor Destroy; override;
        Procedure Free;
        Function GetTag:Integer;
        Procedure BringToFront; virtual;
        Procedure SendToBack; virtual;
        Procedure BringForward; virtual;
        Procedure SendBackward; virtual;
        Function GetX1:Integer;
        Function GetY1:Integer;
        Function GetX2:Integer;
        Function GetY2:Integer;
        Property OnKeyPress : TComponent_cb_KeyPress read GetOnKeyPress write SetOnKeyPress;
        Property OnMouseEnter : TComponent_cb_MouseEnter read GetOnMouseEnter write SetOnMouseEnter;
        Property OnMouseLeave : TComponent_cb_MouseLeave read GetOnMouseLeave write SetOnMouseLeave;
        Property OnClick : TComponent_Focus read GetOnClick write SetOnClick;
        Property OnLoseFocus : TComponent_UnFocus read GetOnLoseFocus write SetOnLoseFocus;
        Property Visible : Boolean read GetVisible write SetVisible;
    end;

implementation

{ TComponent }

constructor TComponent.Create(Parent: TScreen; X1, Y1, X2, Y2: Integer);
begin
     inherited Create;
     Screen:= Parent;
     sX1:= X1;
     sY1:= Y1;
     sX2:= X2;
     sY2:= Y2;
     cb_OnKeyPress:= @_DummyKeyboard;
     cb_OnMouseEnter:= @_DummyMouse;
     cb_OnMouseLeave:= @_DummyMouse;
     cb_OnClick:= @_DummyMouse;
     cb_OnLoseFocus:= @_DummyMouse;
     Tag:= Screen._Register(sX1,sY1,sX2,sY2,@_OnKeyPress,@_OnMouseEnter,@_OnMouseLeave,@_OnClick,@_OnLoseFocus);
     Draw;
end;

destructor TComponent.Destroy;
begin
     Screen._UnRegister(Tag);
     inherited Destroy;
end;

procedure TComponent.Free;
begin
     inherited Free;
end;

function TComponent.GetTag: Integer;
begin
     Result:= Tag;
end;

procedure TComponent.BringToFront;
begin
     Screen.BringToFront(Tag);
end;

procedure TComponent.SendToBack;
begin
     Screen.SendToBack(Tag);
end;

procedure TComponent.BringForward;
begin
     Screen.BringForward(Tag);
end;

procedure TComponent.SendBackward;
begin
     Screen.SendBackward(Tag);
end;

function TComponent.GetX1: Integer;
begin
     Result:= sX1;
end;

function TComponent.GetY1: Integer;
begin
     Result:= sY1;
end;

function TComponent.GetX2: Integer;
begin
     Result:= sX2;
end;

function TComponent.GetY2: Integer;
begin
     Result:= sY2;
end;

procedure TComponent._OnKeyPress(Key: Char);
begin
     cb_OnKeyPress(Key);
end;

procedure TComponent._OnMouseEnter;
begin
     cb_OnMouseEnter;
end;

procedure TComponent._OnMouseLeave;
begin
     cb_OnMouseLeave;
end;

procedure TComponent._OnClick;
Begin
     cb_OnClick;
end;

procedure TComponent._OnLoseFocus;
begin
     cb_OnLoseFocus;
end;

procedure TComponent.SetOnKeyPress(CB: TComponent_cb_KeyPress);
begin
     cb_OnKeyPress:= CB;
end;

function TComponent.GetOnKeyPress: TComponent_cb_KeyPress;
begin
     Result:= cb_OnKeyPress;
end;

procedure TComponent.SetOnMouseEnter(CB: TComponent_cb_MouseEnter);
begin
     cb_OnMouseEnter:= CB;
end;

function TComponent.GetOnMouseEnter: TComponent_cb_MouseEnter;
begin
     Result:= cb_OnMouseEnter;
end;

procedure TComponent.SetOnMouseLeave(CB: TComponent_cb_MouseLeave);
begin
     cb_OnMouseLeave:= CB;
end;

function TComponent.GetOnMouseLeave: TComponent_cb_MouseLeave;
begin
     Result:= cb_OnMouseLeave;
end;

procedure TComponent.SetOnClick(CB: TComponent_Focus);
begin
     cb_OnClick:= CB;
end;

function TComponent.GetOnClick: TComponent_Focus;
begin
     Result:= cb_OnClick;
end;

procedure TComponent.SetOnLoseFocus(CB: TComponent_UnFocus);
begin
     cb_OnLoseFocus:= CB;
end;

function TComponent.GetOnLoseFocus: TComponent_UnFocus;
begin
     Result:= cb_OnLoseFocus;
end;

procedure TComponent.SetVisible(Vis: Boolean);
begin
     Screen.SetVisible(GetTag,Vis);
end;

function TComponent.GetVisible: Boolean;
begin
     Result:= Screen.IsVisible(GetTag);
end;

procedure TComponent._DummyMouse;
begin
     //Do Nothing.
end;

procedure TComponent._DummyKeyboard(Key: Char);
begin
     //Do Nothing.
end;

end.

